<?php

class m_monitor extends CI_Model{
	
	function _list($data){
		extract($data);
		$sql = "SELECT 
                    proyek_kode,
                    proyek_nama,
                    mandor_nama,
                    pembayaran_ket,
                    pekerjaan_ket,
                    cabang_nama,
					m_proyek_id,
					m_mandor_id,
					monitor_periode,
					m_pembayaran_id,
					m_pekerjaan_id,
					monitor_mulai,
					monitor_selesai,
					monitor_wakil,
					monitor_tukang,
					monitor_pekerja,
					monitor_koordinasi,
					monitor_quality,
					monitor_safety,
					monitor_hk,
					monitor_id
				FROM t_monitor
                LEFT JOIN m_proyek ON m_proyek_id = proyek_id
                LEFT JOIN m_mandor ON m_mandor_id = mandor_id
                LEFT JOIN m_pembayaran ON m_pembayaran_id = pembayaran_id
                LEFT JOIN m_pekerjaan ON m_pekerjaan_id = pekerjaan_id
                LEFT JOIN m_cabang ON m_cabang_id = cabang_id
				WHERE 
					1 = 1 
		";
		
        
		if (@$search != ""){
            $sql .= " AND (0 = 1";
            $sql .= " OR proyek_kode like '%".@$search."%'";
            $sql .= " OR proyek_nama like '%".@$search."%'";
            $sql .= " OR mandor_nama like '%".@$search."%'";
            $sql .= " OR pembayaran_ket like '%".@$search."%'";
            $sql .= " OR pekerjaan_ket like '%".@$search."%'";
            $sql .= " OR cabang_nama like '%".@$search."%'";
            $sql .= " OR monitor_periode like '%".@$search."%'";
            $sql .= " OR monitor_mulai like '%".@$search."%'";
            $sql .= " OR monitor_selesai like '%".@$search."%'";
            $sql .= ")";
        }
        
		if (@$m_proyek_id != "") $sql .= " AND m_proyek_id like '%".@$m_proyek_id."%'";
		if (@$m_mandor_id != "") $sql .= " AND m_mandor_id like '%".@$m_mandor_id."%'";
		if (@$monitor_periode != "") $sql .= " AND monitor_periode like '%".@$monitor_periode."%'";
		if (@$m_pembayaran_id != "") $sql .= " AND m_pembayaran_id like '%".@$m_pembayaran_id."%'";
		if (@$m_pekerjaan_id != "") $sql .= " AND m_pekerjaan_id like '%".@$m_pekerjaan_id."%'";
		if (@$monitor_mulai != "") $sql .= " AND monitor_mulai like '%".@$monitor_mulai."%'";
		if (@$monitor_selesai != "") $sql .= " AND monitor_selesai like '%".@$monitor_selesai."%'";
		if (@$monitor_wakil != "") $sql .= " AND monitor_wakil like '%".@$monitor_wakil."%'";
		if (@$monitor_tukang != "") $sql .= " AND monitor_tukang like '%".@$monitor_tukang."%'";
		if (@$monitor_pekerja != "") $sql .= " AND monitor_pekerja like '%".@$monitor_pekerja."%'";
		if (@$monitor_koordinasi != "") $sql .= " AND monitor_koordinasi like '%".@$monitor_koordinasi."%'";
		if (@$monitor_quality != "") $sql .= " AND monitor_quality like '%".@$monitor_quality."%'";
		if (@$monitor_safety != "") $sql .= " AND monitor_safety like '%".@$monitor_safety."%'";
		if (@$monitor_hk != "") $sql .= " AND monitor_hk like '%".@$monitor_hk."%'";
		
		$query = $this->db->query($sql);
		$records = $query->num_rows();
		
		$sql = "SELECT * FROM (".$sql.") k LIMIT ".(($page - 1) * $rows)." , ".$rows;
		$query = $this->db->query($sql);
		$result = $query->result();
		
		return '{ 
		  "total": "'.ceil($records / $rows).'", 
		  "page": "'.$page.'", 
		  "records": "'.$records .'",
		  "rows" : '.json_encode($result).'
		}';
	}
	
	function _insert($data){
		$this->db->insert('t_monitor',$data);
		return $this->db->insert_id();
	}
	
	function _update($data,$id){
		$this->db->where('monitor_id',$id);
		$this->db->update('t_monitor',$data);
		return 1;
	}
	
	function _delete($id){
		$this->db->where('monitor_id',$id);
		$this->db->delete('t_monitor');
		return 1;
	}
	
	function _deactivate($data,$id){
		$this->db->where('monitor_id',$id);
		$this->db->update('t_monitor',$data);
		return 1;
	}
}